<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-button type="primary" @click="showAddDialog()">添加分类</el-button>
      <tree-table
        :data="categoryList"
        :columns="columns"
        show-index
        border
        :expand-type="false"
        :selection-type="false"
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="!scope.row.catDelete" style="color:green"></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>

        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.catLevel === 0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="scope.row.catLevel === 1">二级</el-tag>
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>

        <template slot="operation">
          <el-button size="mini" type="primary" class="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" class="el-icon-delete">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2,5,10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog title="添加分类" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed()">
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules">
        <el-form-item label="分类名称：" label-width="100px" prop="catName">
          <el-input v-model="addForm.catName"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：" label-width="100px">
          <el-cascader
            :options="parentCategoryList"
            :props="cascaderProps"
            v-model="selectedKeys"
            clearable
            @change="parentCategoryChange()"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getCategoryList()
  },
  data() {
    return {
      categoryList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [{
        label: '分类名称',
        prop: 'catName'
      },
      {
        label: '是否有效',
        type: 'template',
        template: 'isok'
      },
      {
        label: '排序',
        type: 'template',
        template: 'order'
      },
      {
        label: '操作',
        type: 'template',
        template: 'operation'
      }
      ],
      addDialogVisible: false,
      addForm: {
        catId: 0,
        catLevel: 0
      },
      addFormRules: {
        catName: [
          { required: true, message: '分类名不能为空', trigger: 'blur' }
        ]
      },
      parentCategoryList: [],
      cascaderProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'catId',
        label: 'catName',
        children: 'children'
      },
      selectedKeys: []
    }
  },
  methods: {
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getCategoryList()
    },
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum
      this.getCategoryList()
    },
    async getCategoryList() {
      const { data: result } = await this.$http.get('/categories', { params: this.queryInfo })
      if (result.status !== 200) {
        return this.$message.error('获取分类数据失败！')
      }

      this.categoryList = result.data.rows
      this.total = result.data.records
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
      this.selectedKeys = []
      this.addForm.catId = 0
      this.addForm.catLevel = 0
    },
    showAddDialog() {
      this.getParentCategoryList()
      this.addDialogVisible = true
    },
    async getParentCategoryList() {
      const { data: result } = await this.$http.get('/categories', { params: { type: 2 } })
      if (result.status !== 200) {
        return this.$message.error('获取父级分类失败！')
      }
      this.parentCategoryList = result.data
    },
    parentCategoryChange() {
      // TODO
    }
  }
}
</script>

<style scoped lang="less">
.el-cascader {
  width: 100%
}

.zk-table {
  margin-top: 15px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
